package com.lsh.util.aliyun.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

/**
 * 阿里云短信服务工具类
 * @author Administrator
 * @date 2019/11/11 18:18
 */
@Component
public class AlySmsUtil {

    private final static String product = "Dysmsapi"; //产品名称
    private final static String domain = "dysmsapi.aliyuncs.com"; //产品域名
    private final static String regionId = "cn-hangzhou"; //产品地区
    private final static String version = "2017-05-25"; //产品版本
    private final static String accessKeyId = "LTAI4FiWP1NcW9izjV1JhKej"; //AK的id
    private final static String accessKeySecret = "HfRjIfLKAwrUKJGsxhEPHAp1B22dEV"; //AK的密钥

    public String sendSms(String telephone, String TemplateParam) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysProduct(product);
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(domain);
        request.setSysVersion(version);
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", telephone);
        request.putQueryParameter("SignName", SmsTemplate.Register.getSignName());
        request.putQueryParameter("TemplateCode", SmsTemplate.Register.getTemplateCode());
        request.putQueryParameter("TemplateParam", "{\"code\" : " + TemplateParam + "}");
//        request.putQueryParameter("SmsUpExtendCode", null);
//        request.putQueryParameter("OutId", null);
        try {
            CommonResponse response = client.getCommonResponse(request);
            String result = response.getData();
            System.out.println(result);
            return result;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
